package com.daji.leetcode;

import org.junit.jupiter.api.Test;

import java.util.*;

public class TestLeetCode {

    @Test
    public void test() {
        System.out.println((asteroidsDestroyed(5,new int[]{4,9,23,4})));

        System.out.println(Arrays.toString(bucketSort(new int[]{2, 3, 2, 4, 54, 5, 56, 6, 6})));
    }

    public boolean asteroidsDestroyed(int mass, int[] asteroids) {
        //对小行星从小到大进行排序
        asteroids = bucketSort(asteroids);
        long masss = mass;
        //Arrays.sort(asteroids);
        for (int i = 0; i < asteroids.length; i++) {
            //masss >= asteroids[i]
            if (masss >= asteroids[i]){
                masss+=asteroids[i];
            }else {
                return false;
            }
        }
        return true;
    }

    public int[] bucketSort(int[] nums) {
        int[] buckets = new int[500001];
        for (int i = 0; i < nums.length; i++) {
            buckets[nums[i]] = nums[i];
        }
        int[] result = new int[nums.length];
        int resultIndex = 0;
        for (int i = 0; i < buckets.length; i++) {
            if (buckets[i] != 0) {
                result[resultIndex] = buckets[i];
                resultIndex++;
            }
        }
        return result;
    }
}